from Tkinter import *
from FrameSerieInfo import FrameSerieInfo
from FrameContentGeneric import FrameContentGeneric

class FrameListGeneric(FrameContentGeneric):

    _series_list = None
    _series_shown = []
    _list = []
    _yScroll = None
    _list = None
    _info = None

    _txt_search = None

    def __init__(self, master):
        Frame.__init__(self,master)

        self._yScroll  =  Scrollbar ( self, orient=VERTICAL )
        self._yScroll.grid ( row=1, column=1, sticky=N+S+E+W)

        self._series_list = Listbox (self, yscrollcommand=self._yScroll.set)
        self._series_list.grid(row = 1, column = 0,sticky=N+S+E+W)

        self._txt_search = Entry (self);
        self._txt_search.delete(0,END)
        self._txt_search.insert(0,'Buscar:')
        self._txt_search.bind('<KeyRelease>', self.search_change)
        self._txt_search.bind('<FocusIn>', self.search_focus_in)
        self._txt_search.bind('<FocusOut>', self.search_focus_out)
        self._txt_search.grid(row = 0, column = 0)

        self._series_list.bind('<ButtonRelease-1>', self.selected_item)

        self._yScroll["command"]  =  self._series_list.yview

    def search_focus_in(self,event):
        if self._txt_search.get() == 'Buscar:':
            self._txt_search.delete(0,END)

    def search_focus_out(self,event):
        if self._txt_search.get() == '':
            self._txt_search.delete(0,END)
            self._txt_search.insert(0,'Buscar:')

    def load_series_list(self,filter):
        filter = filter.lower()
        self._series_list.delete(0,END)
        self._series_shown = []
        for item in self._list:
            if filter == '' or ((str(item[1])).lower().find(filter) != -1):
                self._series_list.insert (END, str(item[1]))
                self._series_shown.append(item)


    def search_change(self, event):
        self.load_series_list(self._txt_search.get())

    def selected_item(self, event):
        selected = self._series_list.curselection()[0]
        selected_id = self._series_shown[int(selected)][0]

        if not(self._info is None):
            self._info.grid_forget()
        self._info = FrameSerieInfo(self, selected_id)
        self._info.grid(column=2, row=0, rowspan=2)

    def load_items(self):
        return None

    def post_load(self):
        self.load_items()
        self.load_series_list('')